-- Server --

-- We changed to implement this inside the interpreter for more usable
-- implementation.  This is left here as history.

server :- server 5151 #

server .port :-
	repeat,
	temporary .pipe,
	concat "nc -q0 -l -p " .port .command,
	stream.popen .pipe .command,
	-- A hole here - what if someone connects but sent nothing?
	stream.peek .pipe ., -- Waits until someone connects in
	spawn (accept .pipe),
	fail #

accept .stream :- process .stream ! accept .stream #
accept .stream :- stream.close .stream #

process .stream :-
	stream.line .stream .str !
	parse .str .parsed,
	generalize .parsed (.var | .query),
	forall .query ( -- Elaboration
		stream.dump .stream .var,
		stream.nl .stream
	),
	stream.put .stream "%FF", -- Denotes end of results
	stream.nl .stream,
	stream.flush .stream #
